import 'package:intl/intl.dart';

class TimeUtil {
  /** 返回当前时间戳 */
  static int currentTimeMillis() {
    return new DateTime.now().millisecondsSinceEpoch;
  }

  /**
   * 参数：2018/12/31
   * 返回：时间戳
   */
  static int getTimestampFromData(String date, {String style = 'yyyy/MM/dd'}) {
    var fortmat = DateFormat(style);
    var time = fortmat.parse(date);
    return time.millisecondsSinceEpoch;
  }

  /**
   * 参数：时间戳
   * 返回：2018/12/31
   */
  static String getFormatTimestamp(String tmestamp, {String mark = '/'}) {
    int time = int.parse(tmestamp);
    var date = DateTime.fromMicrosecondsSinceEpoch(time * 1000);
    var dateStr = '${date.year}${mark}${date.month}${mark}${date.day}';
    return dateStr;
  }
  /**
   * 参数：时间戳
   * 返回：2018/12/31
   */
  static String getFormatTimestamp2(String tmestamp) {
    int time = int.parse(tmestamp);
    var date = DateTime.fromMicrosecondsSinceEpoch(time * 1000);
    var dateStr = '${date.year}年${date.month}月${date.day}日';
    return dateStr;
  }
  /**
   * 参数：时间戳
   * 返回：格式化的时间
   */
  static String getData(int timestamp) {
    var now = new DateTime.now();
    var format = new DateFormat('yyyy-mm-dd HH:mm a');
    var date = new DateTime.fromMicrosecondsSinceEpoch(timestamp * 1000);
    var diff = date.difference(now);
    var time = '';

    if (diff.inSeconds <= 0 ||
        diff.inSeconds > 0 && diff.inMinutes == 0 ||
        diff.inMinutes > 0 && diff.inHours == 0 ||
        diff.inHours > 0 && diff.inDays == 0) {
      time = format.format(date);
    } else {
      if (diff.inDays == 1) {
        time = diff.inDays.toString() + 'DAY AGO';
      } else {
        time = diff.inDays.toString() + 'DAYS AGO';
      }
    }

    return time;
  }
}
